package com.frxuexi.mymvi

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/mymvi/MviActivity")
@AndroidEntryPoint
class MviActivity : AppCompatActivity() {
    val vm by viewModels<MviViewModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mvi)

        lifecycleScope.launch {
            vm.state.collect{
                when(it){
                    is MviState.onSuccess<*> ->{
                        Log.i("lr",it.data.toString())
                    }
                    is MviState.onError->{
                        Log.i("lr",it.msg)
                    }

                    else -> {}
                }
            }
        }

        lifecycleScope.launch{
            vm.channel.send(MviIntent.getNews)
        }
    }
}